<template>
  <div class="regdit">
    <div class="labels">
      <el-button icon="el-icon-s-home" round @click="$router.push('/login')">返回登录</el-button>
    </div>
    <div class="registerLayer">
      <p style="margin: 5px 0 5px 100px;font-family: 'PingFang-RE';
          font-size: 32px;text-align: center;color: #5093d2;letter-spacing: 12px" >找回密码</p>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" style="margin-top: 20px">
        <el-form-item label="邮箱" prop="email">
          <el-input type="text" v-model="ruleForm.email" style="width: 70%"></el-input>
          <el-button type="primary" style="margin-left: 5px" @click="sendEmail">发送信息</el-button>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.password" ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="checkPass" ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="chick-name">
          <el-input type="text" v-model="surenum" style="width: 55% "></el-input>
          <canvas id="canvas"  height="40px"  width="120px" style="float: right;padding:0 15px" @click="getdrawnum"></canvas>
        </el-form-item>
        <el-form-item label="邮箱验证码" prop="chick-name">
          <el-input type="text" v-model="ruleForm.emailNum" style="width: 55% "></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-right: 60px;" @click="SureUser" >提交</el-button>
          <el-button @click="SureUser">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {Draw} from '@/util/DrawNum'
export default {
  name: "Regdit",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      surenum:'',
      num:'',
      checkPass: '',
      ruleForm: {
        password: '',
        email:'',
        emailNum:'',
      },

      rules: {
        name: [
          { trigger: 'blur'}
        ],
        pass: [
          { trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ]
      }
    }
  },
  mounted() {
    this.getdrawnum();
  },
  methods:{
    getdrawnum(){
      var canvas = document.getElementById("canvas");//演员
      var context = canvas.getContext('2d');
      context.clearRect(0, 0, 120, 40);
      this.num=Draw(canvas,context)
    },
    sendEmail(){
      this.$axios.post(
          '/sendEmail',{
            email:this.ruleForm.email
          }
      ).then(resp=>{
        if(resp.data.code===200){
          this.$message({
            type: 'success',
            message:'发送成功,有效期5分钟',
          });
          this.Cookies.set('emailNum',resp.data.emailNum,'5min')
        }
        else{
          this.$message({
            type: 'error',
            message:'请检查邮箱格式',
          });
        }
      })
    },
    SureUser(){
      if(this.ruleForm.emailNum!==this.Cookies.get('emailNum')){
        this.$message({
          type: 'error',
          message:'邮箱验证码错误',
        });
        return ;
      }
      if(this.ruleForm.password!==this.checkPass){
        this.$message({
          type: 'error',
          message:'密码不一致',
        });
        return ;
      }
      if(this.surenum!==this.num){
        this.$message({
          type: 'error',
          message:'验证码不一致',
        });
        return ;
      }
      if(this.ruleForm.password===''){
        this.$message({
          type: 'error',
          message:'密码不能空',
        });
        return ;
      }
      if(this.ruleForm.password.length<6 &&this.ruleForm.password.length<18 ){
        this.$message({
          type: 'error',
          message:'密码长度超过18位。或少于6位',
        });
        return ;
      }
      this.$axios.post(
          '/resetPwd',
          {
            email:this.ruleForm.email,
            pwd:this.ruleForm.password
          }).then(res=>
      {
        if (res.data === 200) {
          this.$message({
            type: "success",
            message: '修改成功,请重新登陆',
          });
          this.Cookies.remove('userId');
          this.Cookies.remove('userName');
          this.Cookies.remove('userImgUrl');
          this.Cookies.remove('userType');
          this.$router.push("/login")
        }
        else
          this.$message({
            type: "error",
            message: '修改失败'
          });
      });

    }
  }
}
</script>

<style scoped>
.registerLayer {
  padding: 35px 50px 0 10px;
  margin: auto;
  width: 550px;
  border-width:  3px;
  border-style: solid ;
  border-color: #2faf8a;
  height: 550px;
  background-color:white;
  border-radius: 32px;
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);
}


.regdit{
  height: 100%;
  width: 100%;
  background-size:100% 100%;
  background-image: linear-gradient(#926db3, #e8cef5, #c330dc);
  min-height: 100vh;
  justify-content: center;

  align-items: center;
}
.el-input{
  border: 1px solid #13d454;
  border-radius: 6px;
}
html,body{
  height: 100%;
}

.labels{
  width: auto;
  top:20px;
  position:absolute;
  left: 44px;
}
</style>
